<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_news
 *
 * @author hoabui
 */
class admin_news extends CI_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        set_validation_error_delimiter();
    }

    function index() {
        $data['subtitle'] = 'Danh sách tin tức';
        $data['site'] = 'tp-admin/site/news/news-listview';
        $page = $this->uri->segment(4);
        if ($page <= 1)
            $page = 0;
        else
            $page = ($page - 1) * PAGE_SIZE;
        $news = $this->news_model->getAllNews_Admin($page);
        $data['list'] = $news['list'];
        $total = $news['total'];

        $link = ADMIN_PAGE_CATEGORY;
        set_paging($link, $total, 4);
        $this->load->view('tp-admin/template', $data);
    }

    function add_news() {
        $data['subtitle'] = 'Thêm tin tức mới';
        $data['site'] = 'tp-admin/site/news/news-add';
        if ($this->input->post('submit') && $this->check_validation()) {
            $info = $this->post_input();
            $info['news_is_delete'] = 0;
            $info['news_id'] = generate_number_code(8);
            $info['news_date'] = date('Y-m-d H:i:s');
            if ($this->news_model->addNews($info)) {
             
                $this->session->set_userdata(array('success_msg' => 'Thêm tin mới thành công'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('error_msg' => 'Thêm tin mới thất bại'));
                redirect(current_url());
            }
        }

        $this->load->view('tp-admin/template', $data);
    }
    
    function update_news() {
        $id=$this->uri->segment(4);
        $data['subtitle'] = 'Cập nhật tin tức';
        $data['site'] = 'tp-admin/site/news/news-update';
        $data['news_info']=$this->news_model->getNewsByID($id);
        if ($this->input->post('submit') && $this->check_validation()) {
            $info = $this->post_input();
            $info['news_date'] = date('Y-m-d H:i:s');
            if ($this->news_model->updateNews($info,$id)) {
             
                $this->session->set_userdata(array('success_msg' => 'Cập nhật tin thành công'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('error_msg' => 'Cập nhật tin thất bại'));
                redirect(current_url());
            }
        }

        $this->load->view('tp-admin/template', $data);
    }
    function delete_news() {
        $id=$this->uri->segment(4);
        $this->news_model->deleteNews($id);
        redirect(HOME_PAGE.ADMIN_PAGE."admin_news");
    }
    function post_input() {
        $info = array(
            'news_title' => $this->input->post('news_title'),
            'news_short_content' => $this->input->post('news_short_content'),
            'news_content' => $this->input->post('news_content'),
            'news_author' => $this->input->post('news_author'),
            'news_source' => $this->input->post('news_source')
        );
        return $info;
    }

    function check_validation() {
        $check_validation = array(
            array(
                'field' => 'news_title',
                'label' => 'Tiêu đề tin tức',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'news_short_content',
                'label' => 'Nội dung ngắn',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'news_content',
                'label' => 'Nội dung',
                'rules' => 'trim|required'
            )
        );
        $this->form_validation->set_rules($check_validation);
        return $this->form_validation->run();
    }

}

?>
